/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.core.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class Description implements Serializable {

	private static final long serialVersionUID = -3519058724331201909L;

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 * 
	 */
	public final class Editor {

		/**
		 * private constructor
		 */
		private Editor() {
		}

		/**
		 * @param comment
		 *            the comment to set
		 */
		public void setComment(String comment) {
			Description.this.comment = comment == null ? DEFAULT_COMMENT
					: comment;
		}

		/**
		 * @param authors
		 *            the authors to set
		 */
		public void setAuthors(List<String> authors) {
			Description.this.authors = authors == null ? new HashSet<String>()
					: new HashSet<String>(authors);

			unmodifiableAuthorsSet = Collections
					.unmodifiableSet(Description.this.authors);
		}

	}

	public static final String DEFAULT_COMMENT = "Just for fun";

	/** comment to this Model */
	private String comment;

	/** authors' names */
	private Set<String> authors;

	/** unmodifiable set backed by the {@link #authors} set */
	private Set<String> unmodifiableAuthorsSet;

	transient private Editor editor;

	/**
	 * default constructor
	 */
	public Description() {
		this(null, null);
	}

	/**
	 * @param authors
	 *            the authors of this model
	 * @param comment
	 *            the comment to set
	 */
	public Description(List<String> authors, String comment) {
		this.authors = authors == null ? new HashSet<String>()
				: new HashSet<String>(authors);
		this.comment = comment == null ? DEFAULT_COMMENT : comment;

		unmodifiableAuthorsSet = Collections.unmodifiableSet(this.authors);
	}

	/**
	 * @return the authors
	 */
	public Set<String> getAuthors() {
		return unmodifiableAuthorsSet;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * @return
	 */
	Editor getEditor() {
		if (editor == null) {
			editor = new Editor();
		}

		return editor;
	}

	@Override
	public String toString() {
		String str = null;
		for (String author : authors) {
			if (str == null) {
				str = "Authors: " + author;
			} else {
				str += ", " + author;
			}
		}
		return (str != null) ? str + "; Comment: " + comment : "Comment: "
				+ comment;
	}

	@Override
	public int hashCode() {
		return comment.hashCode() + authors.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		Description another;
		if (obj instanceof Description) {
			another = (Description) obj;
		} else {
			return false;
		}

		if (!comment.equals(another.comment)) {
			return false;
		}
		if (!authors.equals(another.authors)) {
			return false;
		}

		return true;
	}

}
